package com.ebupt.common.base

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

abstract class BaseViewModel:ViewModel() {

    val jobs = mutableListOf<Job>()

    val isLoading = MutableLiveData<Boolean>()

    fun startAwait(block:suspend CoroutineScope.()->Unit){
        viewModelScope.launch {
            isLoading.value = true
            block.invoke(this)
            isLoading.value = false
        }.addTo(jobs)
    }


    override fun onCleared() {
        jobs.forEach {
            it.cancel()
        }
        super.onCleared()
    }
}

/**
 * 添加到列表
 */
private fun Job.addTo(list: MutableList<Job>){
    list.add(this)
}